import java.io.*;
import java.util.Random;
import org.kwis.msp.lcdui.Card;
import org.kwis.msp.lcdui.Display;
import org.kwis.msp.lcdui.Image;
import org.kwis.msp.lcdui.Graphics;
import org.kwis.msp.lcdui.EventQueue;

class TetrisCard extends Card implements Runnable{
    private Image gameoverImg;
    private Image scoreNumImg[];
    private Image scorePanelImg;
    private Image nextPanelImg;
	private Block block;
	private static int GAMESPEED = 1;
    private BackBoard backBoard;

    public TetrisCard() {
        try {
            gameoverImg = Image.createImage("/gmov.png");       // 이미지 초기화
            scorePanelImg = Image.createImage("/score.png");
           nextPanelImg = Image.createImage("/next.png");
            scoreNumImg = new Image[10];
            for(int i = 0; i < 10; i++)
                scoreNumImg[i] = Image.createImage("/" + i + ".png");
        } catch(IOException e) {
            System.out.println("LOAD Error : " + e);
        }
		block=getBlock();
		backBoard = new BackBoard();
    }
    
    protected boolean keyNotify(int type, int key){
		if (type == EventQueue.KEY_PRESSED) {    // 키코드를 게임액션으로 변환
			keyHandler(Display.getGameAction(key));
			return true;
		}
		return false;
    }

	public Block getBlock() {
        Random random = new Random();
		int blockType = ((int) (Math.abs(random.nextInt()) % 7)) + 1;
		return new Block(blockType);
	}

	private void showScorePanel(Graphics g) {
        g.drawImage(scorePanelImg, BackBoard.MAXWIDTH*6+5, 50, g.LEFT | g.TOP);
    }

    private void showNextPanel(Graphics g) {
        g.drawImage(nextPanelImg, BackBoard.MAXWIDTH*6+5, 10, g.LEFT | g.TOP);
    }
    
    protected  void paint(Graphics g) {
        g.setColor(0, 0, 0);   // 배경 색상 지정(검정색)
        g.fillRect(0, 0, getWidth(), getHeight());   // 배경 색칠
        g.setColor(255, 255, 0);   // 오른쪽 경계선 그리기
        g.drawLine(BackBoard.MAXWIDTH*6+2, 10, BackBoard.MAXWIDTH*6+2, getHeight());
        showScorePanel(g);    // 점수판 그리기
        showNextPanel(g);     // next판 그리기
		backBoard.drawBackBoard(g);
		block.drawBlock(g);
    }

	public void run() {
        try {
            while(true) {
                gameOn();
                Thread.sleep((int) (1000 / GAMESPEED));
            }
        }catch(Exception e) {  }
    }

	protected void keyHandler(int action) {
		
        switch(action) {
            case EventQueue.RIGHT : 
				block.move(1, 0);
				if(!block.moveOk(backBoard))
					block.move(-1, 0);
				else
					repaint();
				break;

            case EventQueue.LEFT : 
			    block.move(-1, 0);
			    if(!block.moveOk(backBoard))
				   block.move(1, 0);
			    else
				   repaint();
			    break;

			case EventQueue.DOWN : 
			    gameOn();
			    break;

			case EventQueue.UP : 
				block.rotate();
				if(!block.moveOk(backBoard))
					   for(int i = 0; i < 3; i++)
							block.rotate();
				else
					   repaint();
				break;
        }
		return;
	}
	
	private void gameOn() {
        block.move(0, 1);
		if(!block.moveOk(backBoard)) {
            block.move(0, -1);
            block.freeze(backBoard);
			checkALine();
			block = getBlock();
		}
		repaint();
    }

	public void checkALine() {
        boolean aline;
        int temp = 0;
        try {
            for(int i = BackBoard.MAXHEIGHT - 1; i >=0; i--) {
                aline = true;
                for(int j = 0; j < BackBoard.MAXWIDTH; j++) {
                    if((backBoard.getBoard())[j][i] == Block.NOCELL)
                        aline = false;
                }
                if(aline) {
                    for(int y = i; y >=1; y--)
                        for(int x = 0; x < BackBoard.MAXWIDTH; x++)
                            (backBoard.getBoard())[x][y] = (backBoard.getBoard())[x][y-1];
                    i++;
                    temp++;
                    repaint();
                }
            }
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
	}
}


